package com.otoe.dilidilivid.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.otoe.dilidilivid.R;
import com.otoe.dilidilivid.data.dao.VideoDao;
import com.otoe.dilidilivid.data.entity.VideoInfo;
import com.otoe.dilidilivid.view.activity.VideoPlayActivity;

import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.VH> {
    private Fragment fragment;
    private Context mContext;
    public VideoListAdapter(Fragment fragment, List<VideoInfo> datas) {
        this.fragment=fragment;
        this.mDatas = datas;
    }
    public VideoListAdapter(Context context, List<VideoInfo> datas) {
        this.mContext=context;
        this.mDatas = datas;
    }

    public static class VH extends RecyclerView.ViewHolder{
        public TextView title;
        public TextView duration;
        public TextView playNum;
        public TextView upName;
        public ImageView show;
        public Context context;
        public VH(View v) {
            super(v);
            title = (TextView) v.findViewById(R.id.tv_title);
            duration = (TextView) v.findViewById(R.id.tv_duration);
            playNum = (TextView) v.findViewById(R.id.tv_play_num);
            upName = (TextView) v.findViewById(R.id.tv_up_name);
            show = (ImageView) v.findViewById(R.id.iv_show);
            context = v.getContext();
        }
    }

    private List<VideoInfo> mDatas;

    @Override
    public void onBindViewHolder(VH holder, int position) {
        holder.title.setText(mDatas.get(position).title);
        holder.duration.setText(mDatas.get(position).duration);
        holder.playNum.setText(""+mDatas.get(position).playNums);
        holder.upName.setText(mDatas.get(position).uname);

        int p=position;
        //holder.show.setImageResource(mDatas.get(position).imgUrl);
        if(fragment!=null){
            Glide.with(fragment).load(mDatas.get(p).getImg()).into(holder.show);
        }
        if(mContext!=null){
            Glide.with(mContext).load(mDatas.get(p).getImg()).into(holder.show);
        }
/*        @SuppressLint("HandlerLeak")
        final Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Bitmap bitmap=(Bitmap) msg.obj;
                holder.show.setImageBitmap(bitmap);
            }

        };
        new Thread(){
            @Override
            public void run(){
                Bitmap bitMap= UrlToBitmap.getVideoThumb(mDatas.get(p).getVideoUrl());
                Message msg=Message.obtain();
                msg.obj=bitMap;
                handler.handleMessage(msg);
            }
        }.start();*/
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //item 点击事件
                Intent intent = null;
                //播放次数加一
                // mDatas.get(p).setPlayNums(mDatas.get(p).getPlayNums()+1);
                // notifyItemChanged(p);
                //同时更新数据库
                new Thread(){
                    @Override
                    public void run(){
                        VideoDao videoDao=new VideoDao();
                        videoDao.addPlayTime(mDatas.get(p));
                    }
                }.start();
                //跳转到播放视频界面
                intent = new Intent(holder.context, VideoPlayActivity.class);
                intent.putExtra("videoUrl",mDatas.get(p).getVideoUrl());
                holder.context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        //LayoutInflater.from指定写法
//         FragmentHomeBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.fragment_home,parent,false);
        View v = View.inflate(parent.getContext(), R.layout.video_list_item, null);
        return new VH(v);
    }

}
